package com.java.controller;

import com.java.pojo.EnterGoodOk;
import com.java.pojo.Maker3;
import com.java.pojo.Page;
import com.java.service.Maker3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/maker3Controller")
public class Maker3Controller {

    @Autowired
    private Maker3Service maker3Service;


    @RequestMapping(value = "/login2", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String s(){
        return "<script>alert('欢迎回来');location.href='/maker3Controller/pageMaker3';</script>";
    }


    /*销售*/
    @RequestMapping("/pageMaker3")
    public String findAll2(@RequestParam(value = "start",required = false) Integer start,
                           @RequestParam(value = "forname",required = false) String forname,
                           Model model){
        if(forname==null ||"".equals(forname)){
            forname="";
        }

        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+forname);
        /*计算总个数*/
        Integer count = maker3Service.getCount1(forname);
        int i = count % 4 ==0? count/4 :(count/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }

        Page<Maker3> page = maker3Service.getPageByPage1(start, forname);
        System.out.println("page:"+page);
        model.addAttribute("page",page);
        model.addAttribute("forname",forname);
        return "maker3/pageMaker3";
    }

    /*退货*/
    @RequestMapping("/THMaker3")
    public String findAll3(@RequestParam(value = "start",required = false) Integer start,
                           @RequestParam(value = "forname",required = false) String forname,
                           Model model){
        if(forname==null ||"".equals(forname)){
            forname="";
        }

        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+forname);
        /*计算总个数*/
        Integer count = maker3Service.getCount0(forname);
        int i = count % 4 ==0? count/4 :(count/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }

        Page<Maker3> page = maker3Service.getPageByPage0(start, forname);
        System.out.println("page:"+page);
        model.addAttribute("page",page);
        model.addAttribute("forname",forname);
        return "maker3/THMaker3Page";
    }


    @RequestMapping(value = "/del",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String del(@RequestParam("m3id") Integer m3id){
        int i = maker3Service.delMaker3(m3id);
        if(i>0){
            return "<script>alert('删除successful');location.href='/maker3Controller/pageMaker3';</script>";
        }else {
            return "<script>alert('删除失败!');location.href='/maker3Controller/pageMaker3';</script>";
        }

    }
}
